package jsimplesim.view.renderers;

import java.awt.*;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;

/**
 *
 * @author ALPHA
 */
public class StackColorCellRenderer extends DefaultTableCellRenderer {
	private static final long serialVersionUID = -2300016389547906314L;
	//private static final Color ansBack = new Color(236, 233, 216);
    final Border rightBorder = BorderFactory.createMatteBorder(0, 0, 0, 2, Color.black);
    final Border leftBorder = BorderFactory.createMatteBorder(0, 2, 0, 0, Color.black);
    private int tableRowHeight;
    private int tableColumn;

    @Override
    public Component getTableCellRendererComponent(JTable table,
            Object value,
            boolean isSelected,
            boolean hasFocus,
            int row,
            int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        tableRowHeight = table.getRowHeight();

        //  Update when the header row is not selected

        if (table.getSelectedRow() != -1) {
            table.getSelectedRow();
            tableColumn = table.getSelectedColumn();
        }

        if (column == 0) // Address
        {
            setHorizontalAlignment(RIGHT);
            setBorder(rightBorder);
            setOpaque(false);
            this.setBackground(setColor());
        } else // Text
        {
            setHorizontalAlignment(LEFT);
            //setBorder( leftBorder );
            setOpaque(false);
            this.setBackground(setColor());
        }

        return this;
    }
    
    public Color setColor() {
		int red = (int) (Math.random() * 255);
		int green = (int) (Math.random() * 255);
		int blue = (int) (Math.random() * 255);
		return new Color(red, green, blue);
	}

	@Override
	public void paintComponent(Graphics g) {
		// Add background highlighting when same row as selected cell row

		g.setColor(setColor());
		Dimension d = getSize();
		g.fillRect(0, 0, d.width, d.height);
		FontMetrics fontMetrics = g.getFontMetrics();
		int width = fontMetrics.charWidth('w');
		int start = width * tableColumn;
		g.fillRect(2 + start, 0, width, tableRowHeight);
		g.setColor(getForeground());
		super.paintComponent(g);
	}
}
